<template>
  <div class="emDialog-container">
    <el-form
      ref="addForm"
      :model="ruleForm"
      :rules="rules"
      class="demo-ruleForm"
    >
      <el-form-item
        label="姓名"
        prop="username"
        label-width="120px"
      >
        <el-input
          v-model="ruleForm.username"
        />
      </el-form-item>
      <el-form-item
        label="手机号"
        prop="mobile"
        label-width="120px"
      >
        <el-input
          v-model="ruleForm.mobile"
        />
      </el-form-item>
      <el-form-item
        label="入职时间"
        prop="timeOfEntry"
        label-width="120px"
      >
        <el-date-picker
          v-model="ruleForm.timeOfEntry"
          type="date"
          placeholder="选择日期"
          style="width: 100%;"
        />
      </el-form-item>
      <el-form-item
        label="工号"
        prop="workNumber"
        label-width="120px"
      >
        <el-input v-model="ruleForm.workNumber" />
      </el-form-item>
      <el-form-item
        label="聘用形式"
        prop="formOfEmployment"
        label-width="120px"
      >
        <el-select
          v-model="ruleForm.formOfEmployment"
          placeholder="请选择"
          style="width:100%"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />

        </el-select>
      </el-form-item>
      <el-form-item
        label="部门名字"
        prop="departmentName"
        label-width="120px"
      >
        <el-input
          v-model="ruleForm.departmentName"
          placeholder="请选择部门"
          style="width:100%"
          @focus="focusFn"
        />
        <div v-show="showTree" class="tree-box">
          <el-tree
            :data="departments"
            :props="{label:'name'}"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item
        label="转正时间"
        label-width="120px"
      >
        <el-date-picker
          v-model="ruleForm.correctionTime"
          type="date"
          placeholder="选择日期"
          style="width: 100%;"
        />
      </el-form-item>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
      style="text-align:right"
    >
      <el-button @click="hEsc">取 消</el-button>
      <el-button
        type="primary"
        @click="hSubmit"
      >确 定</el-button>
    </div>
  </div>
</template>

<script>
// 枚举属性
import EmployeeEnum from '@/constant/employees'
// 获取部门信息的方法
import { getDepartments } from '@/api/departments.js'
import { tranListToTreeData } from '@/utils/index'
import { addEmployees } from '@/api/employees'
const { hireType } = EmployeeEnum
console.log(hireType)
export default {
  name: 'EmDialog',
  // props: {
  //   dialogVisible: {
  //     type: Boolean,
  //     required: true
  //   }
  // },
  data() {
    return {
      // 表单数据
      ruleForm: {
        // 姓名
        username: '123',
        // 手机号
        mobile: '15387169482',
        // 工号
        workNumber: '48956',
        // 部门名字
        departmentName: '行政部',
        // 聘用形式
        formOfEmployment: '',
        // 入职时间
        timeOfEntry: '2000-05-06',
        // 转正时间
        correctionTime: '2000-08-06'
      },
      // 验证规则
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '长度在 1 到 10 个字符',
            trigger: 'blur'
          }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            min: 1,
            max: 11,
            message: '长度在 1 到 11 个字符',
            trigger: 'blur'
          }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' },
          {
            min: 1,
            max: 20,
            message: '长度在 1 到 20 个字符',
            trigger: 'blur'
          }
        ],
        departmentName: [
          { required: true, message: '请输入部门名字', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '长度在 1 到 10 个字符',
            trigger: 'blur'
          }
        ],
        timeOfEntry: [
          { required: true, message: '请输入入职时间', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请输入聘用形式', trigger: 'blur' }
        ]
      },
      // 聘用形式的数据
      hireType,
      // 树形组件数据
      departments: [],
      // 是否展示树形组件
      showTree: false
    }
  },
  created() {
    console.log(EmployeeEnum)
  },
  methods: {
    // 获取所有部门信息
    async loadDepartments() {
      // 调用接口获取部门信息
      const res = await getDepartments()
      // console.log(res)
      // 删除头部元素 不需要的这个元素 这是公司名字
      res.data.depts.shift()
      this.departments = res.data.depts
      // 把得到的数据转为树形结构的
      tranListToTreeData(this.departments)
      // console.log(this.departments)
    },
    // 选择部门的输入框获得焦点
    focusFn() {
      // 输入框获得焦点展示树形组件
      this.showTree = true
      this.loadDepartments()
    },
    // 点击部门
    nodeClick(obj, node) {
      // 有children不处理
      if (obj.children.length) {
        return
      }
      // 没有children
      // 1.数据回填到表单
      this.ruleForm.departmentName = obj.name
      // 2.隐藏tree
      this.showTree = false
    },
    // 点击确认按钮 实现添加
    hSubmit() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          // 1.发送请求 实现添加
          const res = await addEmployees(this.ruleForm)
          console.log(res)
          // 通知父组件清空数据,关闭弹层
          this.$emit('success')
          this.$message.success('添加成功')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 点击取消按钮
    hEsc() {
      // 重置表单
      this.$refs.addForm.resetFields()
      // 关闭弹层
      this.$emit('closeDia')
    }

  }

}
</script>

<style>
</style>
